package cn.edu.neosoft.sunbo509.finefood.Adapter;



import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.edu.neosoft.sunbo509.finefood.BaseUrl;
import cn.edu.neosoft.sunbo509.finefood.Bean.ChangecommentBean;
import cn.edu.neosoft.sunbo509.finefood.Bean.CommentBean;
import cn.edu.neosoft.sunbo509.finefood.R;
import cn.edu.neosoft.sunbo509.finefood.Service.Service;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by 小博 on 2017/11/15.
 */

public abstract class OrderAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<CommentBean>list;
    private Retrofit retrofit;
    private Service service;

    private int order_id;

    public OrderAdapter(Context context) {
        this.context = context;
    }
    public void setList(List<CommentBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View item= LayoutInflater.from(context).inflate(R.layout.item_order,parent,false);
        return new Viewholder(item);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
           Viewholder viewholder=(Viewholder) holder;
           CommentBean date=list.get(position);
           if(date==null)
            return;
                viewholder.tv_shopname.setText(date.getShopname());
                viewholder.tv_dishername.setText(date.getFoodname());
                viewholder.tv_ordertime.setText(date.getOrdertime());
                viewholder.tv_price.setText("￥"+date.getPrice());
                viewholder.btn_comment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        order_id=list.get(position).getOrder_id();
                       showComent();
            }
        });
    }
    @Override
    public int getItemCount() {
        if(list==null)
            return 0;
        else
            return list.size();
    }

    public abstract void showComent();

    //1.2评论框事件
    public  class CommentDialogFragment extends DialogFragment {
        private EditText et_comment;
        private Button btn_comment,btn_cancal;

        private String contain;

        public EditText getEt_comment() {
            return et_comment;
        }

        public void setEt_comment(EditText et_comment) {
            this.et_comment = et_comment;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
            View layoutView = inflater.inflate(R.layout.fragment_comment_dialog, container, false);
            et_comment = (EditText) layoutView.findViewById(R.id.et_commentfood);

            btn_comment=(Button)layoutView.findViewById(R.id.btn_comment);
            btn_cancal=(Button)layoutView.findViewById(R.id.btn_cancal);
            btn_comment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ChangeComment();
                }
            });
            btn_cancal.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            return layoutView;
        }

        //评论菜品
        public void ChangeComment(){
            retrofit=new Retrofit.Builder()
                    .baseUrl(BaseUrl.BASEURL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            service=retrofit.create(Service.class);
            contain=et_comment.getText().toString();
            Call<ChangecommentBean> call=service.addCommentValue(order_id,contain);
            call.enqueue(new Callback<ChangecommentBean>() {
                @Override
                public void onResponse(Call<ChangecommentBean> call, Response<ChangecommentBean> response) {
                    if(response.body().getSuccess().equals("1")) {
                        Toast.makeText(context, "评论成功", Toast.LENGTH_SHORT).show();
                        dismiss();
                    }else {
                        Toast.makeText(context, "评论失败", Toast.LENGTH_SHORT).show();
                    }
                }
                @Override
                public void onFailure(Call<ChangecommentBean> call, Throwable t) {
                    Log.e("error",t.getMessage());
                }
            });
        }

    }



    public class Viewholder extends RecyclerView.ViewHolder {
        private TextView tv_shopname,tv_dishername,tv_ordertime,tv_price;
        private Button btn_comment;
        public Viewholder(View itemView) {
            super(itemView);
            tv_shopname=(TextView)itemView.findViewById(R.id.tv_shopname);
            tv_dishername=(TextView)itemView.findViewById(R.id.tv_dishesname);
            tv_ordertime=(TextView)itemView.findViewById(R.id.tv_ordertime);
            tv_price=(TextView)itemView.findViewById(R.id.tv_price);
            btn_comment=(Button)itemView.findViewById(R.id.btn_comment);
        }
    }
}

